<template>
  <div>
    <el-dialog
      title="提示"
      :visible.sync="passDialogVisible"
      width="30%"
      center
    >
      <div style="text-align:center">
        <span>是否确认审核通过</span>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="passAward">确 定</el-button>
        <el-button @click="passDialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="提示"
      :visible.sync="awaitDialogVisible"
      width="30%"
      center
    >
      <div style="text-align:center">
        <span>是否确认该条成果待定？</span>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="awaitAward">确 定</el-button>
        <el-button @click="awaitDialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="提示"
      :visible.sync="reviseDialogVisible"
      width="30%"
      center
    >
      <div style="text-align:center">
        <span>请输入驳回成果的原因</span>
      </div>
      <el-form :model="form">
        <el-form-item label="驳回原因" :label-width="formLabelWidth">
          <el-select v-model="form.reason">
            <el-option v-for="item in rejectInfo" :key="item.value" :label="item.text" :value="item.value" />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="reviseAward">确 定</el-button>
        <el-button @click="reviseDialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="提示"
      :visible.sync="rejectDialogVisible"
      width="30%"
      center
    >
      <div style="text-align:center">
        <span>请输入拒绝成果的原因</span>
      </div>
      <el-form :model="form">
        <el-form-item label="拒绝原因" :label-width="formLabelWidth">
          <el-select v-model="form.reason">
            <el-option v-for="item in rejectInfo" :key="item.value" :label="item.text" :value="item.value" />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="rejectAward">确 定</el-button>
        <el-button @click="rejectDialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <div class="app-container" style="margin: 30px">
      <el-row :gutter="20">
        <el-col :span="8">
          <div class="block">
            <el-avatar :size="100" :src="UserUrl" />
          </div>
        </el-col>
        <el-col :span="16" style="height: 100px">
          <el-row style="height: 30px">
            {{ stuInfo1 }}
          </el-row>
          <el-row style="height: 30px">
            {{ stuInfo2 }}
          </el-row>
          <el-row style="height: 40px">
            {{ stuInfo3 }}
          </el-row>
          <el-row style="height: 20px">
            <el-button size="small" type="primary" @click="jumpDetail">
              查看详细信息
            </el-button>
          </el-row>
        </el-col>
      </el-row>
    </div>
    <div class="app-container" style="margin: 30px">
      <el-table
        v-loading="loading"
        class="prize-info"
        :data="tableData"
        style="width: 100%; border-radius: 20px"
      >
        <el-table-column
          prop="type"
          width="180"
        />
        <el-table-column
          prop="info"
        />
      </el-table>
    </div>
    <div style="margin: auto; width:auto; text-align: center;">
      <el-button
        type="primary"
        :disabled="btn1"
        icon="el-icon-arrow-left"
        style="margin-right: 60px;"
        plain
        @click="jumpPre"
      >上一条
      </el-button>
      <el-button type="primary" @click="passDialogVisible = true">通 过</el-button>
      <el-button type="primary" @click="awaitDialogVisible = true">待 定</el-button>
      <el-button type="primary" @click="reviseDialogVisible = true">驳回修改</el-button>
      <el-button type="primary" style="margin-right: 60px;" @click="rejectDialogVisible = true">拒 绝</el-button>
      <el-button type="primary" :disabled="btn2" plain @click="jumpNext">下一条<i
        class="el-icon-arrow-right el-icon--right"
      /></el-button>
    </div>
    <div style="margin-top:20px; width:auto;text-align: center;">
      <el-button type="primary" @click="returnReview">返 回</el-button>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import { getToken } from '@/utils/auth'

export default {
  data() {
    return {
      UserUrl: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png',
      stuName: 'XXX',
      // stuNumber: 'XXXXXX',
      loading: true,
      uiIndex: '',
      stuInfo1: '',
      stuInfo2: '',
      stuInfo3: '',
      formLabelWidth: '120px',
      rejectInfo: [],
      passDialogVisible: false,
      awaitDialogVisible: false,
      reviseDialogVisible: false,
      rejectDialogVisible: false,
      pList: [],
      pListPre: [],
      pListNext: [],
      allAcIndex: [],
      acIndices: [],
      btn1: false,
      btn2: false,
      auditStatePre: '',
      auditStateNext: '',
      form: {
        reason: ''
      },
      tableData: [{
        type: '奖项名称',
        info: '奖项1'
      }, {
        type: '发布时间',
        info: '2021-04-30'
      }, {
        type: '活动状态',
        info: '进行中'
      }, {
        type: '是否公示',
        info: '未公示'
      }]
    }
  },
  mounted() {
    if (Array.isArray(this.$route.query.acIndices)) { this.acIndices = this.$route.query.acIndices } else { this.acIndices = [this.$route.query.acIndices] }
    console.log(this.$route.query.acIndices)
    console.log(this.$route.query.acIndex)
    console.log(this.acIndices.findIndex((element) => element === this.$route.query.acIndex))
    this.btn()
    this.fetch()
  },
  methods: {
    fetch() {
      this.$get('achievement/award/queryAdminIdByToken', {}).then((r) => {
        const data = r.data
        this.uiIndex = data.adminId
      })
      this.tableData = []
      // 获取前一个成果的审核状态
      this.pListPre.push({
        nextOrPre: 'pre',
        currentAcIndex: Number(this.$route.query.acIndex),
        acIndices: this.acIndices.map(Number)
      })
      axios({
        method: 'post',
        url: 'http://192.168.1.103:8301/achievement/award/nextPreDetailAwardInformation',
        headers: { Authorization: 'bearer ' + getToken() },
        data: this.pListPre[0]
      }).then((r) => {
        const data = r.data
        this.auditStatePre = data.audit
      })
      // 获取后一个成果的审核状态
      this.pListNext.push({
        nextOrPre: 'next',
        currentAcIndex: Number(this.$route.query.acIndex),
        acIndices: this.acIndices.map(Number)
      })
      axios({
        method: 'post',
        url: 'http://192.168.1.103:8301/achievement/award/nextPreDetailAwardInformation',
        headers: { Authorization: 'bearer ' + getToken() },
        data: this.pListNext[0]
      }).then((r) => {
        const data = r.data
        this.auditStateNext = data.audit
      })
      // 获取当前成果的属性
      this.pList.push({
        acIndex: Number(this.$route.query.acIndex),
        acIndices: this.acIndices.map(Number)
      })
      // 获取驳回信息
      this.$get('achievement/award/getAllRejectInfo', {}).then((r) => {
        const data = r.data.rejectInfos
        for (let i = 0; i < data.length; i++) {
          this.rejectInfo.push(
            {
              text: data[i],
              value: data[i]
            }
          )
        }
      })
      // axios Post方法 其中body中的数据赋给data
      axios({
        method: 'post',
        url: 'http://192.168.1.103:8301/achievement/award/queryAwardInformationByAcIndex',
        headers: { Authorization: 'bearer ' + getToken() },
        data: this.pList[0]
      }).then((r) => {
        const data = r.data
        console.log('当前的data如下：')
        console.log(data)
        this.stuNumber = data.siNumber
        this.allAcIndex = data.allAcIndices
        this.stuInfo1 = data.studentBasicInfo.sbiName + ' | ' + data.studentBasicInfo.sbiNumber + ' | ' + data.studentBasicInfo.sbiGroup
        this.stuInfo2 = data.studentBasicInfo.sbiAcademy + ' | ' + data.studentBasicInfo.sbiDepartment
        this.stuInfo3 = data.studentBasicInfo.sbiMajor + ' | ' + data.studentBasicInfo.sbiClass
        // if (data.awardLevel) {
        //   this.tableData.push(
        //     {
        //       type: '成果等级',
        //       info: data.awardLevel
        //     }
        //   )
        // }
        if (data.scoringAspect) {
          this.tableData.push(
            {
              type: '成果类型',
              info: data.scoringAspect + ' -- ' + data.scoringItem + ' -- ' + data.awardLevel
            }
          )
        }
        if (data.audit) {
          this.tableData.push(
            {
              type: '成果审核状态',
              info: data.audit
            }
          )
        }
        for (let i = 0; i < data.attribute.length; i++) {
          this.tableData.push(
            {
              type: data.attribute[i],
              info: data.value[i]
            }
          )
        }
        this.loading = false
      })
    },
    passAward() {
      this.passDialogVisible = false
      this.$get('achievement/award/updateAwardAudit', {
        uiIndex: this.uiIndex,
        auditResult: '同意',
        acIndex: this.$route.query.acIndex
      }).then((r) => {
        this.$router.push({
          path: '/award/reviewed_award',
          query: {
            acIndex: this.$route.query.acIndex,
            acIndices: [this.$route.query.acIndex]
          }
        })
        // this.$router.push({path: '/award/review'
      }
      )
    },
    awaitAward() {
      this.awaitDialogVisible = false
      this.$get('achievement/award/updateAwardAudit', {
        uiIndex: this.uiIndex,
        auditResult: '待定',
        acIndex: this.$route.query.acIndex
      }).then((r) => {
        this.$router.push({
          path: '/award/review_award',
          query: {
            acIndex: this.$route.query.acIndex,
            acIndices: [this.$route.query.acIndex]
          }
        })
        // this.$router.push({path: '/award/review'
      })
    },
    reviseAward() {
      this.reviseDialogVisible = false
      this.$get('achievement/award/updateAwardAudit', {
        uiIndex: this.uiIndex,
        auditResult: '驳回',
        acIndex: this.$route.query.acIndex,
        rejectInfo: this.form.reason
      }).then((r) => {
        this.reviseDialogVisible = false
        this.$router.push({
          path: '/award/reviewed_award',
          query: {
            acIndex: this.$route.query.acIndex,
            acIndices: [this.$route.query.acIndex]
          }
        })
        // this.$router.push({path: '/award/review'
      })
    },
    rejectAward() {
      this.rejectDialogVisible = false
      this.$get('achievement/award/updateAwardAudit', {
        uiIndex: this.uiIndex,
        auditResult: '拒绝',
        acIndex: this.$route.query.acIndex,
        rejectInfo: this.form.reason
      }).then((r) => {
        this.$router.push({
          path: '/award/reviewed_award',
          query: {
            acIndex: this.$route.query.acIndex,
            acIndices: [this.$route.query.acIndex]
          }
        })
        // this.$router.push({path: '/award/review'})
      })
    },
    jumpDetail() {
      this.$router.push({ path: '/student/single-info', query: { sbiNumber: this.stuNumber }})
    },
    btn() {
      const currentIndex = this.acIndices.findIndex((element) => element === this.$route.query.acIndex)
      if (currentIndex === 0 && this.acIndices.length === 1) {
        this.btn1 = true
        this.btn2 = true
      } else if (currentIndex === 0) {
        this.btn1 = true
        this.btn2 = false
      } else if (currentIndex === this.acIndices.length - 1) {
        this.btn1 = false
        this.btn2 = true
      } else {
        this.btn1 = false
        this.btn2 = false
      }
    },
    jumpPre() {
      const currentIndex = this.acIndices.findIndex((element) => element === this.$route.query.acIndex)
      if (this.auditStateNext === '未审核' || this.auditStateNext === '已审核待定') {
        this.$router.push({
          path: '/award/review_award',
          query: {
            acIndex: this.acIndices[currentIndex - 1],
            acIndices: this.allAcIndex
          }
        })
        window.location.reload(true)
      } else {
        this.$router.push({
          path: '/award/reviewed_award',
          query: {
            acIndex: this.acIndices[currentIndex - 1],
            acIndices: this.allAcIndex
          }
        })
      }
    },
    jumpNext() {
      const currentIndex = this.acIndices.findIndex((element) => element === this.$route.query.acIndex)
      if (this.auditStateNext === '未审核' || this.auditStateNext === '已审核待定') {
        this.$router.push({
          path: '/award/review_award',
          query: {
            acIndex: this.acIndices[currentIndex + 1],
            acIndices: this.allAcIndex
          }
        })
        window.location.reload(true)
      } else {
        this.$router.push({
          path: '/award/reviewed_award',
          query: {
            acIndex: this.acIndices[currentIndex + 1],
            acIndices: this.allAcIndex
          }
        })
      }
    },
    returnReview() {
      this.$router.push({ path: '/award/review' })
    }
  }
}

</script>

<style scoped>
@import "SelectType.scss";
</style>
